﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace pgen
{
    [Serializable]
    public class Settings
    {
        public int PinLength { get; set; }
        public int PinCount { get; set; }
        public int SerialLength { get; set; }
        public int SerialStart { get; set; }

        public Settings()
        {
            PinLength = 8;
            PinCount = 1;
            SerialLength = 4;
            SerialStart = 1000;
        }

        public void SerializeToXml(string filename)
        {
            var serializer = new XmlSerializer(typeof(Settings));
            using (var textWriter = new StreamWriter(filename))
            {
                serializer.Serialize(textWriter, this);
            }
        }

        public static Settings DeserializeFromXml(string filename)
        {
            if (!File.Exists(filename))
                return new Settings();
            
            Settings settings;
            var deserializer = new XmlSerializer(typeof(Settings));
            using (var textReader = new StreamReader(filename))
            {
                settings = (Settings)deserializer.Deserialize(textReader);
            }
            return settings;
        }
    }
}
